package com.jfsnpm.authority;


import java.util.Date;

import org.apache.commons.lang.StringUtils;


import org.snaker.jfinal.plugin.SnakerPlugin;

import com.jfinal.core.ActionInvocation;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfsnpm.common.AppLog;
import com.jfsnpm.common.AppSql;



public class AuthorityCheck {
	/**
	 * 验证登陆信息
	 * @param c
	 * @return
	 */
	public static boolean isLogin(Controller c){
		String userid , userpassword ;
		userid 		= c.getSessionAttr("userid");
		if(StringUtils.isBlank(userid)){
			//无session，验证cookie
			userid  = c.getCookie("userid");
			userpassword = c.getCookie("userpassword");
			if(StringUtils.isBlank(userid)||StringUtils.isBlank(userpassword)){
				return false;//无cookie
			}
			//验证cookie
			return checkpsw(userid,userpassword,c);
		}else{
			return true;
		}
	}
	/**
	 * 验证ID密码并写入session
	 * @param id
	 * @param password
	 * @param c
	 * @return
	 */
	public static boolean checkpsw(String id , String password,Controller c){
		Record user = Db.findById("pm_user", id);
		if(user==null) return false;
		if(StringUtils.equals(user.getStr("password"),password)){
			Date exp = user.getDate("expirytime");
			boolean expyn=false;
			if(exp==null){
				expyn=true;
			}else{
				expyn = new Date().before(exp);
			}
			if(expyn&&StringUtils.equals(user.getStr("status"), "启用")){
				c.setSessionAttr("userid", user.getStr("id"));
				c.setSessionAttr("userno", user.getStr("no"));
				c.setSessionAttr("username", user.getStr("name"));
				c.setSessionAttr("usermail", user.getStr("mail"));
				return true;
			}
		}
		return false;
	}
	/**
	 * 验证 用户NO 密码并写入session
	 * @param flag 任意值
	 * @param no
	 * @param password
	 * @param c
	 * @return
	 */
	public static boolean checkpsw(String flag,String no , String password,Controller c,String remember){
		Record user = Db.findFirst(AppSql.getsql("pm.getUserByNo"), no);
		if(user==null) return false;
		if(StringUtils.equals(user.getStr("password"),password)){
			Date exp = user.getDate("expirytime");
			boolean expyn=false;
			if(exp==null){
				expyn=true;
			}else{
				expyn = new Date().before(exp);
			}
			if(expyn&&StringUtils.equals(user.getStr("status"), "启用")){
				c.setSessionAttr("userid", user.getStr("id"));
				c.setSessionAttr("userno", user.getStr("no"));
				c.setSessionAttr("username", user.getStr("name"));
				c.setSessionAttr("usermail", user.getStr("mail"));
				if(!StringUtils.isBlank(remember)){
					//写入cookie
					c.setCookie("userid", user.getStr("id"), 604800);
					c.setCookie("userpassword", user.getStr("password"), 604800);
				}
				return true;
			}
		}
		return false;
	}
	/**
	 *清除session
	 * @param c
	 */
	public static void logout(Controller c){
		c.setSessionAttr("userid", "");
		c.setSessionAttr("userno", "");
		c.setSessionAttr("username", "");
		c.setSessionAttr("usermail", "");
	}
	/**
	 * 修改密码
	 * @param id 用户ID
	 * @param password 老密码
	 * @param newpassword 新密码
	 * @return
	 */
	public static boolean changepsw(String id , String password , String newpassword){
		Record user = Db.findById("pm_user", id);
		if(user==null) return false;
		if(StringUtils.equals(user.getStr("password"),password)){
			user.set("password", newpassword);
			return Db.update("pm_user", user);
		}
		return false;
	}
	/**
	 * 获得URL权限
	 * @param path
	 * @param userid
	 * @return
	 */
	public static boolean getPathAuthoriyByUserid(String path,String userid){
		if(StringUtils.isBlank(path)||StringUtils.isBlank(userid)) return false;
		if(StringUtils.equals(userid, "admin")) return true;
		Record cc = Db.findFirst(AppSql.getsql("pm.getPathAuthoriyByUserid"),StringUtils.substringAfter(path, "/"),userid);
		return Long.parseLong(cc.get("cc").toString()) > 0 ;
	}
	/**
	 * 获得URL权限
	 * @param ai
	 * @return
	 */
	public static boolean getPathAuthoriy(ActionInvocation ai){
		String userid = ai.getController().getSessionAttr("userid").toString();
		String path = ai.getActionKey();
		if(StringUtils.startsWith(path,"/Index")) return true;
		if(StringUtils.startsWith(path, "/Login")) return true;
		
		// XXX:作为演示去除修改密码功能
		if(StringUtils.equals(path,"/Login/changepwdcheck")) return false;

		if(getPathAuthoriyByUserid(path, userid)){
			//记录访问信息
			AppLog.setLog(path, userid);
			return true;
		}else{
			return false;
		}
	}
	/**
	 * 获得流程启动权限
	 * @param userid
	 * @param processid
	 * @return
	 */
	public static boolean getProcessAuthoriy(String userid , String processid){
		if(StringUtils.isBlank(processid)||StringUtils.isBlank(userid)) return false;
		if(StringUtils.equals(userid, "admin")) return true;
		Record cc = Db.findFirst(AppSql.getsql("pm.getProcessAuthoriyByUserid"),processid,userid);
		return Long.parseLong(cc.get("cc").toString()) > 0 ;
	}
	/**
	 * 获得任务权限
	 * @param userid
	 * @param taskid
	 * @return
	 */
	public static boolean getTaskAuthoriy(String userid , String taskid){
		if(StringUtils.isBlank(taskid)||StringUtils.isBlank(userid)) return false;
		if(StringUtils.equals(userid, "admin")) return true;
		if(StringUtils.startsWithAny(userid,SnakerPlugin.getEngine().query().getTaskActorsByTaskId(taskid))){
			return true;
		}
		return false;
	}

	
	
	
}
